package com.bw.module_service.ui

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_storage.mmkv.AuthMMKV
import com.bw.module_service.R
import com.bw.module_service.databinding.ActivityAddParkSpaceBinding
import com.bw.module_service.utils.ToastUtils
import com.bw.module_service.viewmodel.ServiceViewModel
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
@Route(path = Router.Ui.AddParkSpaceActivity)
class AddParkSpaceActivity : BaseActivity<ServiceViewModel, ActivityAddParkSpaceBinding>() {

    var area = ""
    var type = ""
    private val mViewModel: ServiceViewModel by viewModels()
    override val mLayoutResId: Int
        get() = R.layout.activity_add_park_space

    private fun commitaddspace(){
        if(AuthMMKV.token==""){
            ToastUtils.showLong(this, "请先登录")
            ARouter.getInstance().build(Router.Ui.LoginActivity).navigation()
            return
        }
        val map = mutableMapOf<String, Any>()
        //  "areaId": 6,
        //  "areaName": "A园区C栋",
        //  "id": 0,
        //  "licensePlate": "津AB1BBB",
        //  "remarks": "ewqq",
        //  "spaceNumber": "A199",
        //  "spaceStatus": "空闲",
        //  "spaceType": "标准车位",
        //  "userId": 10,
        //  "userName": "小红"
        map["areaId"] = 6
        map["areaName"] = area
        map["id"] = 100
        map["licensePlate"] = mBinding.addspaceCarnum.text.toString()
        map["remarks"] = mBinding.addspaceBeizhu.text.toString()
        map["spaceNumber"] = mBinding.addspaceNum.text.toString()
        val selectedStatusId = mBinding.addspceRg.checkedRadioButtonId
        val status = when (selectedStatusId) {
            R.id.rb_kx -> "空闲"
            R.id.rb_zy -> "已占用"
            R.id.rb_wh -> "维护中"
            else -> "空闲"
        }
        map["spaceStatus"] = status
        map["spaceType"] = type
        map["userId"] = 10
        map["userName"] = mBinding.addspaceOwner.text.toString()
        val json = Gson().toJson(map)
        val body = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        mViewModel.addParkSpace(body)
    }
    override fun initData() {
        //提交
        mBinding.duigou.setOnClickListener {
            commitaddspace()
        }
        mBinding.addCarspace.setOnClickListener {
            commitaddspace()
        }

        //退出
        mBinding.back.setOnClickListener {
            finish()
        }

    }

    override fun initListener() {
        mViewModel._addParkSpaceLiveData.observe(this) {
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show()
            finish()
        }
    }


    override fun initView() {
        val arr = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        mBinding.addspaceArea.adapter = adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.addspaceArea.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area = arr[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

        val arrType = arrayOf("标准车位", "VIP车位")
        val adapter2 = ArrayAdapter(this, android.R.layout.simple_spinner_item, arrType)
        mBinding.addspaceType.adapter = adapter2
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.addspaceType.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type = arrType[position]

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }
    }


}